apply plugin: 'com.android.application'

android {
    compileSdk 28

    defaultConfig {
        applicationId 'com.baidu.paddle.paddlenlp.app'
        minSdkVersion 15
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation project(path: ':ernie_tiny')
    implementation project(path: ':ui')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    //noinspection GradleDependency
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    implementation 'org.jetbrains:annotations:15.0'
    //noinspection GradleDependency
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

def FD_MODEL = [
        [
                'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ernie-tiny.tgz',
                'dest': 'src/main/assets/models'
        ],
        [
                'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ernie-tiny-clip.tgz',
                'dest': 'src/main/assets/models'
        ],
        [
                'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ernie-tiny-clip-qat.tgz',
                'dest': 'src/main/assets/models'
        ],
        [
                'src' : 'https://bj.bcebos.com/paddlehub/fastdeploy/ernie-tiny-clip-qat-embedding-int8.tgz',
                'dest': 'src/main/assets/models'
        ]

]

task downloadAndExtractModels(type: DefaultTask) {
    doFirst {
        println "[INFO] Downloading and extracting models ..."
    }
    doLast {
        String cachePath = "cache"
        if (!file("${cachePath}").exists()) {
            mkdir "${cachePath}"
        }
        FD_MODEL.eachWithIndex { model, index ->
            String[] modelPaths = model.src.split("/")
            String modelName = modelPaths[modelPaths.length - 1]
            String modelPrefix = modelName.substring(0, modelName.length() - 4)
            boolean copyFiles = false
            if (!file("${model.dest}/${modelPrefix}").exists()) {
                // Download the target model if not exists
                if (!file("${cachePath}/${modelName}").exists()) {
                    println "[INFO] Downloading ${model.src} -> ${cachePath}/${modelName}"
                    ant.get(src: model.src, dest: file("${cachePath}/${modelName}"))
                }
                copyFiles = true
            }
            if (copyFiles) {
                println "[INFO] Taring ${cachePath}/${modelName} -> ${model.dest}/${modelPrefix}"
                copy { from(tarTree("${cachePath}/${modelName}")) into("${model.dest}") }
            } else {
                println "[INFO] ${model.dest}/${modelPrefix} already exists!"
            }
        }
    }
}

preBuild.dependsOn downloadAndExtractModels
